//
//  ZDTabBar.m
//  OCSinaWeiBo
//
//  Created by Apple on 15/10/27.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDTabBar.h"
#import "UIView+ZUtility.h"

@interface ZDTabBar ()

@property (nonatomic, strong) UIButton *composeButton;

@end

@implementation ZDTabBar


/**
 *  重新布局子控件
 */
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    ZDLog(@"%@", self.subviews);   //打印tabBar的子控件  可以结合view的层次结构来认识tabBar的内部组成
    
    CGFloat width = self.width / 5;
    CGFloat height = self.height;
    
    NSInteger index = 0;
    
    for (UIView *view in self.subviews)
    {
        //由于tabBarItem本质上是UITabBarButton，这是一个苹果没有公开的类，我们没法使用，所以这里选择其父类来判断
        if ([view isKindOfClass:[UIControl class]])
        {
            view.frame = CGRectMake(index * width, 0, width, height);
            
            index += index == 1 ? 2 : 1;
        }
    }
    
    //设置中间按钮的frame
    self.composeButton.frame = CGRectMake(width * 2, 0, width, height);
}


#pragma mark 懒加载
- (UIButton *)composeButton
{
    if (!_composeButton)
    {
        _composeButton = [[UIButton alloc] init];
        
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_composeButton setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_composeButton setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        
        [self addSubview:_composeButton];   //如果在layoutSubViews中添加 会执行多次 影响性能
    }
    
    return _composeButton;
}


/*  中间按钮可以绑定事件 通知代理实现控制器跳转  也可以通过block实现  */


@end
